package com.platform.service;

import com.platform.dao.ApiCouponGoodsMapper;
import com.platform.dao.ApiCouponMapper;
import com.platform.entity.CouponGoodsEntity;
import com.platform.entity.CouponVo;
import com.platform.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class ApiCouponService {
    @Autowired
    private ApiCouponMapper apiCouponMapper;
    @Autowired
    private ApiCouponGoodsMapper apiCouponGoodsMapper;

    public CouponVo queryObject(Integer couponId) {
        return apiCouponMapper.queryObject(couponId);
    }

    public List<CouponVo> queryList(Map<String, Object> map) {
        return apiCouponMapper.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return apiCouponMapper.queryTotal(map);
    }


    public void save(CouponVo userVo) {
        apiCouponMapper.save(userVo);
    }

    public void update(CouponVo user) {
        apiCouponMapper.update(user);
    }

    public void delete(Long userId) {
        apiCouponMapper.delete(userId);
    }

    public void deleteBatch(Long[] userIds) {
        apiCouponMapper.deleteBatch(userIds);
    }

    public List<CouponVo> queryUserCoupons(Map<String, Object> map) {
        // 检查优惠券是否过期
        List<CouponVo> couponVos = apiCouponMapper.queryUserCoupons(map);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(date);

        List<CouponGoodsEntity> couponGoodsList = null;
        for (CouponVo couponVo : couponVos) {
            //优惠券状态 1 可用 2 已用 3 过期
            if (couponVo.getCoupon_status() == 1) {
                //查询用户优惠券是否过期
                if (1 == DateUtils.compareDateTime(couponVo.getUCouponUseEndDate(), dateNowStr)) {
                    couponVo.setCoupon_status(3);
                    apiCouponMapper.updateUserCoupon(couponVo);
                }
            }
            //满减(后台发放)验证coupon表的use_end_date有没有过期
            if (couponVo.getCoupon_status() == 3) {
                //查询用户优惠券是否过期
                if (-1 == DateUtils.compareDateTime(couponVo.getUCouponUseEndDate(), dateNowStr)) {
                    couponVo.setCoupon_status(3);
                    apiCouponMapper.updateUserCoupon(couponVo);
                }
            }
        }
        List<CouponVo> newCouponVos = apiCouponMapper.queryUserCoupons(map);
        for (CouponVo couponVo: newCouponVos) {
            Map goodsMap = new HashMap();
            goodsMap.put("couponId",couponVo.getId());
            List<CouponGoodsEntity> list = apiCouponGoodsMapper.queryCouponGoodsByCouponId(goodsMap);
            List<Integer> couponGoodsId = list.stream().map(CouponGoodsEntity::getGoodsId).collect(Collectors.toList());
            couponVo.setGoodsId(couponGoodsId);
        }
        return newCouponVos;
    }

    public CouponVo queryMaxUserEnableCoupon(Map<String, Object> map) {
        return apiCouponMapper.queryMaxUserEnableCoupon(map);
    }

    public List<CouponVo> queryUserCouponList(Map<String, Object> map) {
        return apiCouponMapper.queryUserCouponList(map);
    }


}
